package cn.itcast.controller;

import cn.hutool.poi.excel.ExcelWriter;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Business;
import cn.itcast.pojo.Report;
import cn.itcast.service.ReportService;
import cn.itcast.vo.FindMemberCountVo;
import cn.itcast.vo.FindSetmealVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 报表
 */
@RestController
@RequestMapping("/report")
@CrossOrigin
public class ReportController {

    @Reference
    private ReportService reportService;


    /**
     * 每月收入统计
     *
     * @return
     */
    @GetMapping("/income")
    public Result selectIncome() {
        try {
            Report report = reportService.selectIncome();
            return new Result(true, MessageConstant.QUERY_INCOME_SUCCESS, report);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_INCOME_FAIL);
        }
    }

    /**
     * 预约/支付统计
     */
    @GetMapping("/order")
    public Result selectOrder() {
        try {
            Report report = reportService.selectOrder();
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, report);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_SUCCESS);
        }
    }

    /**
     * 运营数据统计
     *
     * @param business
     * @return
     */
    @GetMapping("/business")
    public Result selectBusiness(Business business) {
        try {
            Business business1 = reportService.selectBusiness(business);
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, business1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @GetMapping("/member/count")
    public Result findMemberCount(){
        try {
            FindMemberCountVo vo=reportService.findMemberCount();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,vo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @GetMapping("/setmeals")
    public Result findSetmeals(){
        List<FindSetmealVo> list=reportService.findSetmeals();
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,list);
    }

//    @RequestMapping("/export/business")
//    public void exportExcel(HttpServletResponse response, Business business) {
//        try {
//            Business result = reportService.selectBusiness(business);
//            String fileName = "report_" + result.getReportDate() + ".xlsx";
//            FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\PAMO\\Desktop\\woshinibaba\\javaee131-itcast-health\\itcast-health-parent\\itcast-health-backend\\src\\main\\resources\\static\\template\\report_template.xlsx"));
//            //模板路径创建
//            OutputStream outputStream = response.getOutputStream();
//            response.setContentType("application/vnd.ms-excel");
//            response.setCharacterEncoding("utf-8");
//            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//            ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(fileInputStream).build();
//
//            WriteSheet writeSheet = EasyExcel.writerSheet().build();
//            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//            excelWriter.fill(result, writeSheet);
//            excelWriter.fill(result.getHotSetmeal(), fillConfig, writeSheet);
//            outputStream.flush();
//            excelWriter.finish();
//            outputStream.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
